package com.canaan.business.controller.pc.inOutStock;

import com.canaan.business.domain.inOutStock.WmsOutStockDetailOrder;
import com.canaan.business.domain.vo.OutStockDetailOrderAvailableQtyVo;
import com.canaan.business.domain.vo.distributePickerVo;
import com.canaan.business.service.inOutStock.IWmsOutStockDetailOrderService;
import com.canaan.common.annotation.Log;
import com.canaan.common.core.controller.BaseController;
import com.canaan.common.core.domain.AjaxResult;
import com.canaan.common.core.page.TableDataInfo;
import com.canaan.common.enums.BusinessType;
import com.canaan.common.exception.ServiceException;
import com.canaan.common.utils.StringUtils;
import com.canaan.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 出库单明细Controller
 *
 * @author ruoyi
 * @date 2022-12-20
 */
@RestController
@Api(tags = "出库单明细")
@RequestMapping("/inOutStock/outStockDetailOrder")
public class WmsOutStockDetailOrderController extends BaseController
{
    @Resource
    private IWmsOutStockDetailOrderService wmsOutStockDetailOrderService;

    /**
     * 查询出库单明细列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询出库单明细列表")
    public TableDataInfo list(WmsOutStockDetailOrder wmsOutStockDetailOrder)
    {
        startPage();
        List<WmsOutStockDetailOrder> list = wmsOutStockDetailOrderService.selectWmsOutStockDetailOrderList(wmsOutStockDetailOrder);
        return getDataTable(list);
    }

    /**
     * 导出出库单明细列表
     */
    @Log(title = "出库单明细", businessType = BusinessType.EXPORT)
    @ApiOperation(value = "出库单明细")
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsOutStockDetailOrder wmsOutStockDetailOrder)
    {
        List<WmsOutStockDetailOrder> list = wmsOutStockDetailOrderService.selectWmsOutStockDetailOrderList(wmsOutStockDetailOrder);
        ExcelUtil<WmsOutStockDetailOrder> util = new ExcelUtil<WmsOutStockDetailOrder>(WmsOutStockDetailOrder.class);
        util.exportExcel(response, list, "出库单明细数据");
    }

    /**
     * 获取出库单明细详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取出库单明细详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(wmsOutStockDetailOrderService.selectWmsOutStockDetailOrderById(id));
    }

    /**
     * 新增出库单明细
     */
    @ApiOperation(value = "新增出库单明细")
    @Log(title = "出库单明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WmsOutStockDetailOrder wmsOutStockDetailOrder)
    {
        return toAjax(wmsOutStockDetailOrderService.insertWmsOutStockDetailOrder(wmsOutStockDetailOrder));
    }

    /**
     * 修改出库单明细
     */
    @ApiOperation(value = "修改出库单明细")
    @Log(title = "出库单明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WmsOutStockDetailOrder wmsOutStockDetailOrder)
    {
        return toAjax(wmsOutStockDetailOrderService.updateWmsOutStockDetailOrder(wmsOutStockDetailOrder));
    }

    /**
     * 删除出库单明细
     */
    @Log(title = "出库单明细", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除出库单明细")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(wmsOutStockDetailOrderService.deleteWmsOutStockDetailOrderByIds(ids));
    }


    /**
     * 分配拣货
     */
    @Log(title = "分配拣货", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "分配拣货")
    @PutMapping("/distributePickerTask")
    public AjaxResult distributePickerTask(@RequestBody distributePickerVo entity)
    {
        if(entity == null){
            throw new ServiceException("缺少参数");
        }else if(StringUtils.isEmpty(entity.getPicker())){
            throw new ServiceException("缺少拣货员");
        }else if(StringUtils.isEmpty(entity.getStorehouseNo())){
            throw new ServiceException("缺少仓库");
        }
        return toAjax(wmsOutStockDetailOrderService.distributePickerTask(entity));
    }


    @Log(title = "确认分配", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "确认分配")
    @PutMapping("/confirmDistribute/{id}")
    public AjaxResult confirmDistribute(@PathVariable("id") Long id){
        return toAjax(wmsOutStockDetailOrderService.confirmDistribute(id));
    }

    @Log(title = "根据仓库获取库位可用数量")
    @ApiOperation(value = "根据仓库获取可用数量")
    @GetMapping("/availableQty")
    public AjaxResult outStockDetailOrderAvailableQty(OutStockDetailOrderAvailableQtyVo vo){
        return success(wmsOutStockDetailOrderService.outStockDetailOrderAvailableQty(vo));
    }
}
